<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
 <head>
  <meta http-equiv="content-type" content="text/html; charset=UTF-8">
  <title>Register one or more global variables with the current session</title>

 </head>
 <body><div class="manualnavbar" style="text-align: center;">
 <div class="prev" style="text-align: left; float: left;"><a href="function.session-register-shutdown.html">session_register_shutdown</a></div>
 <div class="next" style="text-align: right; float: right;"><a href="function.session-save-path.html">session_save_path</a></div>
 <div class="up"><a href="ref.session.html">Session 函数</a></div>
 <div class="home"><a href="index.html">PHP Manual</a></div>
</div><hr /><div id="function.session-register" class="refentry">
 <div class="refnamediv">
  <h1 class="refname">session_register</h1>
  <p class="verinfo">(PHP 4, PHP 5 &lt; 5.4.0)</p><p class="refpurpose"><span class="refname">session_register</span> &mdash; <span class="dc-title">Register one or more global variables with the current session</span></p>

 </div>
 
 <div class="refsect1 description" id="refsect1-function.session-register-description">
  <h3 class="title">说明</h3>
  <div class="methodsynopsis dc-description">
   <span class="type">bool</span> <span class="methodname"><strong>session_register</strong></span>
    ( <span class="methodparam"><span class="type"><a href="language.pseudo-types.html#language.types.mixed" class="type mixed">mixed</a></span> <code class="parameter">$name</code></span>
   [, <span class="methodparam"><span class="type"><a href="language.pseudo-types.html#language.types.mixed" class="type mixed">mixed</a></span> <code class="parameter">$...</code></span>
  ] )</div>

  <p class="para rdfs-comment">
   <span class="function"><strong>session_register()</strong></span> accepts a variable number of
   arguments, any of which can be either a string holding the name of a
   variable or an array consisting of variable names or other arrays. For
   each name, <span class="function"><strong>session_register()</strong></span> registers the global
   variable with that name in the current session.
  </p>
  <p class="para">
   You can also create a session variable by simply setting the
   appropriate member of the <var class="varname"><var class="varname"><a href="reserved.variables.session.html" class="classname">$_SESSION</a></var></var>
   or <var class="varname"><var class="varname">$HTTP_SESSION_VARS</var></var> (PHP &lt; 4.1.0) array.
   <div class="informalexample">
    <div class="example-contents">
<div class="phpcode"><code><span style="color: #000000">
<span style="color: #0000BB">&lt;?php<br /></span><span style="color: #FF8000">//&nbsp;Use&nbsp;of&nbsp;session_register()&nbsp;is&nbsp;deprecated<br /></span><span style="color: #0000BB">$barney&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #DD0000">"A&nbsp;big&nbsp;purple&nbsp;dinosaur."</span><span style="color: #007700">;<br /></span><span style="color: #0000BB">session_register</span><span style="color: #007700">(</span><span style="color: #DD0000">"barney"</span><span style="color: #007700">);<br /><br /></span><span style="color: #FF8000">//&nbsp;Use&nbsp;of&nbsp;$_SESSION&nbsp;is&nbsp;preferred,&nbsp;as&nbsp;of&nbsp;PHP&nbsp;4.1.0<br /></span><span style="color: #0000BB">$_SESSION</span><span style="color: #007700">[</span><span style="color: #DD0000">"zim"</span><span style="color: #007700">]&nbsp;=&nbsp;</span><span style="color: #DD0000">"An&nbsp;invader&nbsp;from&nbsp;another&nbsp;planet."</span><span style="color: #007700">;<br /><br /></span><span style="color: #FF8000">//&nbsp;The&nbsp;old&nbsp;way&nbsp;was&nbsp;to&nbsp;use&nbsp;$HTTP_SESSION_VARS<br /></span><span style="color: #0000BB">$HTTP_SESSION_VARS</span><span style="color: #007700">[</span><span style="color: #DD0000">"spongebob"</span><span style="color: #007700">]&nbsp;=&nbsp;</span><span style="color: #DD0000">"He's&nbsp;got&nbsp;square&nbsp;pants."</span><span style="color: #007700">;<br /></span><span style="color: #0000BB">?&gt;</span>
</span>
</code></div>
    </div>

   </div>
  </p>
  <p class="para">
   If <span class="function"><a href="function.session-start.html" class="function">session_start()</a></span> was not called before this function
   is called, an implicit call to <span class="function"><a href="function.session-start.html" class="function">session_start()</a></span> with no
   parameters will be made.  <var class="varname"><var class="varname"><a href="reserved.variables.session.html" class="classname">$_SESSION</a></var></var> does not mimic
   this behavior and requires <span class="function"><a href="function.session-start.html" class="function">session_start()</a></span> before use.
  </p>
  <div class="warning"><strong class="warning">Warning</strong><p class="simpara">本函数已自 PHP 5.3.0 起<em class="emphasis">废弃</em>并将自
PHP 5.4.0 起<em class="emphasis">移除</em>。</p></div>
 </div>


 <div class="refsect1 parameters" id="refsect1-function.session-register-parameters">
  <h3 class="title">参数</h3>
  <p class="para">
   <dl>

    
     <dt>
<em><code class="parameter">name</code></em></dt>

     <dd>

      <p class="para">
       A string holding the name of a variable or an array consisting of
       variable names or other arrays.
      </p>
     </dd>

    
    
     <dt>
<em><code class="parameter">...</code></em></dt>

     <dd>

      <p class="para">
      </p>
     </dd>

    
   </dl>

  </p>
 </div>


 <div class="refsect1 returnvalues" id="refsect1-function.session-register-returnvalues">
  <h3 class="title">返回值</h3>
  <p class="para">
   成功时返回 <strong><code>TRUE</code></strong>， 或者在失败时返回 <strong><code>FALSE</code></strong>。
  </p>
 </div>


 <div class="refsect1 notes" id="refsect1-function.session-register-notes">
  <h3 class="title">注释</h3>
  <div class="caution"><strong class="caution">Caution</strong>
   <p class="para">
    If you want your script to work regardless of <a href="ini.core.html#ini.register-globals" class="link">register_globals</a>, you need to
    instead use the <var class="varname"><var class="varname"><a href="reserved.variables.session.html" class="classname">$_SESSION</a></var></var> array as
    <var class="varname"><var class="varname"><a href="reserved.variables.session.html" class="classname">$_SESSION</a></var></var> entries are automatically registered. If
    your script uses <span class="function"><strong>session_register()</strong></span>, it will not work
    in environments where the PHP directive <a href="ini.core.html#ini.register-globals" class="link">register_globals</a> is disabled.
   </p>
  </div>
  <blockquote class="note"><p><strong class="note">Note</strong>: <strong>register_globals 重要说明：</strong><br />
<p class="para">自 PHP 4.2.0 起，PHP 指令
 <a href="ini.core.html#ini.register-globals" class="link">register_globals</a> 的默认值为 <em class="emphasis">off</em>。PHP
 社区鼓励开发者不要依赖于此指令, 用其他手段替代，例如<a href="language.variables.predefined.html" class="link">superglobals</a>。</p></p></blockquote>
  <div class="caution"><strong class="caution">Caution</strong>
   <p class="para">
    This registers a <em class="emphasis">global</em> variable. If you want to
    register a session variable from within a function, you need to make sure
    to make it global using the <a href="language.variables.scope.html" class="link"><strong class="command">global</strong></a>
    keyword or the <var class="varname"><var class="varname"><a href="reserved.variables.globals.html" class="classname">$GLOBALS[]</a></var></var> array, or use the special
    session arrays as noted below.
   </p>
  </div>
  <div class="caution"><strong class="caution">Caution</strong>
   <p class="para">
    If you are using <var class="varname"><var class="varname"><a href="reserved.variables.session.html" class="classname">$_SESSION</a></var></var>
    (or <var class="varname"><var class="varname">$HTTP_SESSION_VARS</var></var>), do not use
    <span class="function"><strong>session_register()</strong></span>,
    <span class="function"><a href="function.session-is-registered.html" class="function">session_is_registered()</a></span>, and
    <span class="function"><a href="function.session-unregister.html" class="function">session_unregister()</a></span>.
   </p>
  </div>
  <blockquote class="note"><p><strong class="note">Note</strong>: 
   <p class="para">
    It is currently impossible to register resource variables in a session.
    For example, you cannot create a connection to a database and store the
    connection id as a session variable and expect the connection to still be
    valid the next time the session is restored.  PHP functions that return a
    resource are identified by having a return type of
    <em>resource</em> in their function definition.  A list of
    functions that return resources are available in the <a href="resource.html" class="link">resource types</a> appendix.
   </p>
   <p class="para">
    If <var class="varname"><var class="varname"><a href="reserved.variables.session.html" class="classname">$_SESSION</a></var></var> (or <var class="varname"><var class="varname">$HTTP_SESSION_VARS</var></var>
    for PHP 4.0.6 or less) is used, assign values to
    <var class="varname"><var class="varname"><a href="reserved.variables.session.html" class="classname">$_SESSION</a></var></var>. For example: $_SESSION[&#039;var&#039;] = &#039;ABC&#039;;
   </p>
  </p></blockquote>
 </div>


 <div class="refsect1 seealso" id="refsect1-function.session-register-seealso">
  <h3 class="title">参见</h3>
  <p class="para">
   <ul class="simplelist">
    <li class="member"><span class="function"><a href="function.session-is-registered.html" class="function" rel="rdfs-seeAlso">session_is_registered()</a> - 检查变量是否在会话中已经注册</span></li>
    <li class="member"><span class="function"><a href="function.session-unregister.html" class="function" rel="rdfs-seeAlso">session_unregister()</a> - Unregister a global variable from the current session</span></li>
    <li class="member"><var class="varname"><var class="varname"><a href="reserved.variables.session.html" class="classname">$_SESSION</a></var></var></li>
   </ul>
  </p>
 </div>


</div><hr /><div class="manualnavbar" style="text-align: center;">
 <div class="prev" style="text-align: left; float: left;"><a href="function.session-register-shutdown.html">session_register_shutdown</a></div>
 <div class="next" style="text-align: right; float: right;"><a href="function.session-save-path.html">session_save_path</a></div>
 <div class="up"><a href="ref.session.html">Session 函数</a></div>
 <div class="home"><a href="index.html">PHP Manual</a></div>
</div></body></html>
